package com.rjkj.medicalwaste.ui.storage

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.rjkj.medicalwaste.bean.GarbageStatus
import com.rjkj.medicalwaste.bean.MedicalWasteType
import com.rjkj.medicalwaste.bean.ReviewWeightState
import com.rjkj.medicalwaste.database.GarbageSetBoxed
import com.rjkj.medicalwaste.database.GarbageSetUnReview
import com.rjkj.medicalwaste.databinding.DialogUnreviewWeightBinding
import com.rjkj.medicalwaste.utils.TimeUtils
import com.rjkj.medicalwaste.viewmodels.ReviewWeightViewModel
import com.rjkj.medicalwaste.viewmodels.UnBoxedViewModel
import com.rjkj.medicalwaste.viewmodels.UserViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class UnReviewWeightDialog : DialogFragment() {

    private val unBoxedViewModel: UnBoxedViewModel by viewModels {
        MainViewModelProvider.provideUnBoxedModel(requireContext())
    }
    private val userViewModel: UserViewModel by activityViewModels {
        MainViewModelProvider.provideUserModel(requireContext())
    }

    private val garbageViewModel: ReviewWeightViewModel by viewModels {
        MainViewModelProvider.providerReviewWeightModel(requireContext(), arguments?.getString(BoxListFragment.GARBAGE_TYPE)
                ?: "")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val binding = DialogUnreviewWeightBinding.inflate(inflater, container, false)
        binding.garbage = garbageViewModel
        binding.lifecycleOwner = viewLifecycleOwner
        onSubscribeUi(binding)
        return binding.root
    }

    fun onSubscribeUi(binding: DialogUnreviewWeightBinding) {
        binding.imClose.setOnClickListener {
            dismiss()
        }
        garbageViewModel.garbageBean.observe(viewLifecycleOwner){value->
            lifecycleScope.launchWhenResumed {
                Log.e("TAG", "onSubscribeUi: $value")
                if (value != null) {
                    lifecycleScope.launch{
                        unBoxedViewModel.updateGarbageSetUnReview(GarbageSetUnReview(
                                id = value.id,
                                garbageQRCode = value.garbageQRCode,
                                garbageUserStatus = GarbageStatus.review,
                                reviewWeightState = ReviewWeightState.CheckSuccess,
                                tempBoxQRCode = getTempBoxCode(userViewModel.stagingRoomUser.value?.qrcode?:"",
                                        MedicalWasteType.getTypeByTypeName(value.garbageType).num),
                                reviewDeviceSN = userViewModel.getSerial(),
                                reviewWeight = "0",
                                reviewType = "0",
                                reviewTime = TimeUtils.getYMDHMSTime(),
                        ))


                    }
                    delay(3000L)
                    dismiss()
                }
            }


        }


    }
    private fun getTempBoxCode(roomId:String,type:Int):String{
        return "A$type$roomId"
    }
    override fun onStart() {
        dialog!!.window!!.apply {
            attributes.gravity = Gravity.CENTER//位于底部
            decorView.background = ColorDrawable(Color.TRANSPARENT)//背景色
        }
        super.onStart()
    }


}